package logic

import (
	"context"

	"monitor/user/api/internal/svc"
	"monitor/user/api/internal/types"
	"monitor/user/model"

	"github.com/tal-tech/go-zero/core/logx"
)

type TermOfflineLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTermOfflineLogic(ctx context.Context, svcCtx *svc.ServiceContext) TermOfflineLogic {
	return TermOfflineLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TermOfflineLogic) TermOffline(req types.PageReq, token string) (*types.StatusReplyS, error) {
	// todo: add your logic here and delete this line

	claims := ParseToken(token)
	//分页查询
	reply := []types.StatusReply{}
	count, termStatus, err := l.svcCtx.StatusModel.FindAllOffline(req.Page, req.Limit, claims["city"].(string))
	//fmt.Printf("count:'%d,userInfos:%#v,err = %v\n", count, userInfos, err)
	switch err {
	case nil:
		for _, val := range termStatus {
			//t, _ := time.Parse("2006-01-02", val.Ndate)
			//fmt.Printf("index = %d,userInfo:%#v\n", index, val)
			rep := types.StatusReply{
				Id:     val.Id,
				Sn:     val.Sn,
				Posno:  val.Posno,
				City:   val.City,
				TyId:   val.Tyid,
				Sver:   val.Sver,
				Unum1:  uint(val.Unum1),
				Unum2:  uint(val.Unum2),
				Ndate:  val.Ndate[0:10],
				Ntime:  val.Ntime,
				Amount: uint(val.Amount),
				Count:  uint(val.Count),
				Line:   uint(val.Line),
				Carno:  val.Carno,
				Jd:     val.Jd,
				Wd:     val.Wd,
				Alarm:  int(val.Alarm),
				Pver:   uint(val.Pver),
				Bver:   uint(val.Bver),
				Empid:  val.Empid,
				Rvs1:   val.Rvs1,
				Rvs2:   val.Rvs2,
				Stime:  val.Stime,
				Ctime:  val.Ctime,
			}
			reply = append(reply, rep)
		}
		return &types.StatusReplyS{Count: count, Reply: reply}, nil
	case model.ErrNotFound:
		return nil, errorRecNotFound
	default:
		return nil, err
	}
}
